package main

import (
	"flag"
	"fmt"
	"gitee.com/visons/gopkg/logger"
	"gitee.com/visons/gopkg/router"
	"github.com/nats-io/go-nats"
)

func ckeckErr(err error) {
	if err != nil {
		logger.Fatal(err)
	}
}
func main() {
	var (
		natsUrl, serverAddress, groupAddress string
	)

	flag.StringVar(&natsUrl, "url", nats.DefaultURL, "nats url: nats://localhost:4222")
	flag.StringVar(&serverAddress, "serverAddress", "default.1", "self server address: default.1")
	flag.StringVar(&groupAddress, "groupAddress", "default", "self group address: default")

	flag.Parse()

	fmt.Printf("url %s\n serverAddress %s\n groupAddress %s\n", natsUrl, serverAddress, groupAddress)

	natsRouter, err := router.ConnectNats(natsUrl)
	ckeckErr(err)
	natsRouter.SetMsgHandler(func(remoteServerAddress string, msg []byte) {
		logger.Printf("receive msg %s from %s\n", string(msg), remoteServerAddress)
	})

	natsRouter.RegisterServer(serverAddress, groupAddress)

	fmt.Println("remoteAddress msg")
	var address, msg string
	for {
		fmt.Scanf("%s %s", &address, &msg)
		fmt.Println(msg)
		natsRouter.SendMsg(address, []byte(msg))
	}
}
